package cn.edu.jiangnan.cashmanagement.web.user;


import java.io.Serializable;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.edu.jiangnan.cashmanagement.service.user.IUserService;
import cn.edu.jiangnan.cashmanagement.util.FacesUtils;

@Component
@Scope(value="session")
public class LoginBean implements Serializable{

	private static final long serialVersionUID = 1L;

	@Inject
	@Named(value="authenticationManager")
    private AuthenticationManager am;

    private String nameError;

    @Autowired
    private IUserService userService;

    public String getNameError() {
        return nameError;
    }
    public void setNameError(String nameError) {
        this.nameError = nameError;
    }

    public String login() {

        try {
            Authentication request = new UsernamePasswordAuthenticationToken(this.getUserName(), getPassword());
            Authentication result = am.authenticate(request);
            result.getAuthorities();
            SecurityContextHolder.getContext().setAuthentication(result);
            ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
            ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            return "/main.xhtml?faces-redirect=true";

        } catch (AuthenticationException e) {
        	e.printStackTrace();
            FacesUtils.addErrorString(null, "login.failed");
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new FacesException(e);
        }
    }

    private String userName;

    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void validateName(ValueChangeEvent e) {
        // UIInput nameInput = e.getComponent();
        String name = (String) e.getNewValue();

        if (name.contains("_"))
            nameError = "Name cannot contain underscores";
        else if (name.equals(""))
            nameError = "Name cannot be blank";
        else
            nameError = "";
    }

    
    
    public void validate(ComponentSystemEvent e) {
        UIComponent form = e.getComponent();

        FacesContext fc = FacesContext.getCurrentInstance();
        UIInput newPass = (UIInput) form.findComponent("newPass");

        if (true)
            fc.renderResponse();
    }
  

}
